package com.huluobo.composedemo2006a

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.huluobo.composedemo2006a.ui.theme.ComposeDemo2006aTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeDemo2006aTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
//                    Greeting("Android")
//                    Imageing()
//                    News()
//                    val list = arrayListOf<NewsData>()
//                    //循环相当于网络请求获取数据
//                    for (i in 0..30) {
//                        list.add(
//                            NewsData("这是新闻的标题$i", "这是新闻的内容$i", R.mipmap.ic_launcher, "这是图片的描述")
//                        )
//                    }
//
//                    //相当于给adapter进行赋值
//                    News(newsData = list)
                    PhotographerCard()
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String) {
    //Column是纵向排列的组件
//    Column {
//        Text(
//            text = "Hello $name!",
//            textAlign = TextAlign.Start,
//            color = Color.Red,
//            fontSize = 20.sp
//        )
//        Text(text = "Hello Compose", textAlign = TextAlign.End, color = Color.Red, fontSize = 20.sp)
//    }
}

//@Composable
//fun News() {
////    Column {
////        for (i in 0..3) {
////            Row(Modifier.padding(20.dp)) {
////                Image(
////                    painter = painterResource(id = R.mipmap.ic_launcher),
////                    contentDescription = "这是一张图片"
////                )
////                Column(Modifier.padding(horizontal = 20.dp)) {
////                    Text(text = "这是新闻标题", fontSize = 20.sp, color = Color.Black)
////                    Text(text = "这是新闻的内容", fontSize = 18.sp, color = Color.DarkGray)
////                }
////            }
////        }
////    }
//
//    LazyColumn(content = {
//        for (i in 0..30) {
//            item {
//                Row(Modifier.padding(20.dp)) {
//                    Image(
//                        painter = painterResource(id = R.mipmap.ic_launcher),
//                        contentDescription = "这是一张图片"
//                    )
//                    Column(Modifier.padding(horizontal = 20.dp)) {
//                        Text(text = "这是新闻标题", fontSize = 20.sp, color = Color.Black)
//                        Text(text = "这是新闻的内容", fontSize = 18.sp, color = Color.DarkGray)
//                    }
//                }
//            }
//        }
//    })
//}

class NewsData(
    var title: String,
    var content: String,
    var imageResource: Int,
    var imageContent: String
)

//构建新闻数据的view(item)
//相当于在adapter中构建viewHolder的布局
@Composable
fun newsView(newsData: NewsData) {
    Row(Modifier.padding(20.dp)) {
        Image(
            painter = painterResource(id = newsData.imageResource),
            contentDescription = newsData.imageContent
        )
        Column(Modifier.padding(horizontal = 20.dp)) {
            Text(text = newsData.title, fontSize = 20.sp, color = Color.Black)
            Text(text = newsData.content, fontSize = 18.sp, color = Color.DarkGray)
        }
    }
}

//构建整个Recyclerview,相当于在Android中给Recyclerview添加适配器
@Composable
fun News(newsData: List<NewsData>) {
    LazyColumn(content = {
        for (i in newsData.indices) {
            item {
                newsView(newsData = newsData[i])
            }
        }
    })
}

@Composable
fun Image() {
    Image(painter = painterResource(id = R.mipmap.ic_launcher_round), contentDescription = "这是一张图片")
}

@Composable
@Preview(showBackground = true)
fun PhotographerCard() {
//    Column {
//        val user = ActiveUser.current //通过current方法,取出当前的值
//        Text(text = user.name, fontWeight = FontWeight.Bold)
//        CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {//透明度为0.75
//            Text(text = user.lastActiveTime, style = MaterialTheme.typography.body2)
//        }
//        CompositionLocalProvider(ActiveUser provides User("小红", "5分钟前")) {
//            val newUser = ActiveUser.current
//            Text(newUser.name, fontWeight = FontWeight.Bold)
//            CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {//透明度为0.75除以3/4
//                Text(text = user.lastActiveTime, style = MaterialTheme.typography.body2)
//            }
//        }
//    }
    Row {
        Surface( //Surface 可以理解为是一个容器,可以设置高度宽度,形状,背景等等
            modifier = Modifier.size(50.dp),//设置大小
            shape = CircleShape,//设置形状
            color = MaterialTheme.colors.onSurface.copy(alpha = 0.2f)//设置色值
        ) {
            //加载网络图片放在这,可以在这个盒子中显示一个图片
//            Image(
//                painter = painterResource(id = R.mipmap.ic_launcher),
//                contentDescription = null
//            )
        }

        Column {
            val user = ActiveUser.current
            Text(user.name, fontWeight = FontWeight.Bold)
            CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {
                Text(text = user.lastActiveTime, style = MaterialTheme.typography.body2)
            }
        }
    }
}

val ActiveUser = compositionLocalOf {
    User("小明", "3分钟前")
}

data class User(val name: String, val lastActiveTime: String)